<!--
 * @Author: xuzehan 123@qq.com
 * @Date: 2023-05-30 14:35:10
 * @LastEditors: xuzehan 935351619@qq.com
 * @LastEditTime: 2023-08-09 14:29:55
 * @FilePath: \ant-design-vue-jeecg-master\src\views\taskManterminalCountsment\modules\createDate.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div>
    <!-- <a-modal
      title="配置指控车"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      width="1200px"
      @cancel="handleCancel"
    >-->
    <a-button
      class="editable-add-btn"
      @click="handleAdd"
      type="primary"
      :style="{ 'margin-bottom': '5px' }"
    >添加</a-button>
    <a-table :scroll="{ x: true }"   class="j-table-force-nowrap"  bordered :data-source="dataSource" :columns="columns" :pagination="false" >
      <template slot="name" slot-scope="text, record">
        <a-input v-model="record.name"></a-input>
        <!-- <a-input v-model="record.commandVehicleId"></a-input> -->
        <!-- <a-select
          v-model="record.commandVehicleId"
          placeholder="请选择指控车"
          @change="changeCar(record)"
          :style="{ width: '100%' }"
        >
          <a-select-option :value="item.id" v-for="item in carList" :key="item.id">{{ item.name }}</a-select-option>
        </a-select>-->
      </template>
      <template slot="networkAccessCode" slot-scope="text, record">
        <a-input v-model="record.networkAccessCode" readonly></a-input>
      </template>
      <template slot="identificationCode" slot-scope="text, record">
        <a-input v-model="record.identificationCode" readonly></a-input>
      </template>

      <template slot="useResourcePriority" slot-scope="text, record">
        <!-- <a-input v-model="record.useResourcePriority"></a-input> -->
        <j-dict-select-tag
          :style="{ width: '100%' }"
          type="list"
          v-model="record.useResourcePriority"
          dictCode="use_resource_priority"
          placeholder="请选择"
        />
      </template>
      <template slot="missileUpChannelsNumber" slot-scope="text, record">
        <a-input v-model="record.missileUpChannelsNumber" placeholder="请输入"></a-input>
      </template>

      <template slot="planApplyMissileTotal" slot-scope="text, record">
        <a-input v-model="record.planApplyMissileTotal" placeholder="请输入"></a-input>
      </template>

      <template slot="missileUpRate" slot-scope="text, record">
<!--        <a-input suffix="kbps" v-model="record.missileUpRate" placeholder="请输入111"></a-input>-->
        <a-select v-model="record.missileUpRate" @change="changeMissileUpRate" >
          <a-select-option value="1.5">1.5kbps</a-select-option>
          <a-select-option value="3">3kbps</a-select-option>
          <a-select-option value="6">6kbps</a-select-option>
        </a-select>
      </template>
      <template slot="receiverType" slot-scope="text, record">
        <a-select v-model="record.receiverType"  @change="chenggeReceiverType(record.receiverType)">
          //切换统一  待改
                    <a-select-option value="L">L接收机</a-select-option>
                    <a-select-option value="KA">KA接收机</a-select-option>
                  </a-select>
      </template>
      <template slot="missileDownRate" slot-scope="text, record">
        <!-- <a-input suffix="kbps" v-model="record.missileDownRate" placeholder="请输入"></a-input> -->
        <a-select v-model="record.missileDownRate" @change="changeRate" >
                    <a-select-option value="00">下行3ksps</a-select-option>
                    <a-select-option value="11">下行12ksps</a-select-option>
                    <a-select-option value="22">下行24ksps</a-select-option>
                    <a-select-option value="33">下行48ksps</a-select-option>
                    <a-select-option value="44">下行96ksps</a-select-option>
                  </a-select>
      </template>
      <template slot="missileDownFrequency" slot-scope="text, record">
        <a-select v-model="record.missileDownFrequency" style="width:140px" @change="changeFrequency" >

          <a-select-option :value="item.value" v-for="item in pointList" :key="item.value">{{item.label}}</a-select-option>
          <!-- <a-select-option value="00">频点0（935MHz）</a-select-option>
                    <a-select-option value="11">频点1（940MHz）</a-select-option>
                    <a-select-option value="22">频点2（945MHz）</a-select-option>
                    <a-select-option value="33">频点3（950MHz）</a-select-option>
                    <a-select-option value="44">频点4（955MHz）</a-select-option>
                    <a-select-option value="55">频点5（960MHz）</a-select-option>
                    <a-select-option value="66">频点6（965MHz）</a-select-option>
                    <a-select-option value="77">频点7（970MHz）</a-select-option>
                    <a-select-option value="88">频点8（975MHz）</a-select-option>
                    <a-select-option value="99">频点9（980MHz）</a-select-option>
                    <a-select-option value="AA">频点10（985MHz）</a-select-option> -->
                  </a-select>
        <!-- <a-input
          v-model="record.missileDownFrequency"
          placeholder="请输入"
          :style="{ width: '100%' }"
          suffix="M"
        /> -->
      </template>
      <!-- <template slot="title" slot-scope="text, record">
          <a-input v-model="record.title"></a-input>
      </template>-->

      <template slot="operation" slot-scope="text, record">
        <a-popconfirm
          v-if="dataSource.length"
          title="确定删除该数据?"
          overlayClassName="poperLayTable"
          @confirm="() => onDelete(record.key)"
        >
          <a href="javascript:;">删除</a>
        </a-popconfirm>
      </template>
    </a-table>
    <!-- </a-modal> -->
  </div>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
const EditableCell = {
  template: `
      <div class="editable-cell">
        <div v-if="editable" class="editable-cell-input-wrapper">
          <a-input :value="value" @change="handleChange" @pressEnter="check" /><a-icon
            type="check"
            class="editable-cell-icon-check"
            @click="check"
          />
        </div>
        <div v-else class="editable-cell-text-wrapper">
          {{ value || ' ' }}
          <a-icon type="edit" class="editable-cell-icon" @click="edit" />
        </div>
      </div>
    `,
  props: {
    text: String,
  },
  data() {
    return {
      value: this.text,
      editable: false,
    }
  },
  methods: {

    showModal() {
      this.visible = true
    },
    handleChange(e) {
      const value = e.target.value
      this.value = value
    },
    check() {
      this.editable = false
      this.$emit('change', this.value)
    },
    edit() {
      this.editable = true
    },
  },
}
export default {
  components: {
    EditableCell,
  },
  watch: {
    count: {
      handler(newVal, oldVal) {
        if (newVal) {
          console.log(newVal, 'newVal')
          this.$emit('setCount', newVal)
          // // this.dataSource = []
          // for (let i = 0; i < newVal; i++) {
          //   this.dataSource.push({
          //     key: i,
          //     commandVehicleId: '',
          //     missileDownFrequency: '1515',
          //     useResourcePriority: '1',
          //     missileUpChannelsNumber: '4',
          //     networkAccessCode: '',
          //     identificationCode: '',
          //     missileDownRate: '96',
          //     planApplyMissileTotal: '',
          //     missileUpRate: ''
          //   })

          // }
        }
      },
      immediate: true,
    }
  },
  data() {
    return {
      count: 0,
      url: {
        list: '/commandVehicle/nmcCommandVehicle/list',
        config: '/fightDemandNcv/nmcFightDemandNcv/addBatch',
        getConfig: '/fightDemandNcv/nmcFightDemandNcv/queryFightDemandCommandVehicle',
      },
      carList: [],
      visible: false,
      confirmLoading: false,
      pointList:[
          {label:'1475MHz',value:'00'},
          {label:'1480MHz',value:'11'},
          {label:'1485MHz',value:'22'},
          {label:'1490MHz',value:'33'},
          {label:'1495MHz',value:'44'},
          {label:'1500MHz',value:'55'},
          {label:'1505MHz',value:'66'},
          {label:'1510MHz',value:'77'},
          {label:'1515MHz',value:'88'},
          {label:'1520MHz',value:'99'},
          {label:'1525MHz',value:'AA'},
      ],
      pointLists:{
        list1:[
          {label:'1475MHz',value:'00'},
          {label:'1480MHz',value:'11'},
          {label:'1485MHz',value:'22'},
          {label:'1490MHz',value:'33'},
          {label:'1495MHz',value:'44'},
          {label:'1500MHz',value:'55'},
          {label:'1505MHz',value:'66'},
          {label:'1510MHz',value:'77'},
          {label:'1515MHz',value:'88'},
          {label:'1520MHz',value:'99'},
          {label:'1525MHz',value:'AA'},
        ],
        list2:[]
      },
      dataSource: [
        // {
        //   key: '1',
        //   commandVehicleId: '指控车2',
        //   code: '0002',
        //   missileDownFrequency: '3',
        // },
      ],
      // count: 0,
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 70,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '指控车',
          dataIndex: 'name',
          width: 160,
          scopedSlots: { customRender: 'name' },
        },
        {
          title: '车入网认证码',
          dataIndex: 'networkAccessCode',
          width: 130,
          scopedSlots: { customRender: 'networkAccessCode' },
        },
        {
          title: '车辆识别码',
          dataIndex: 'identificationCode',
          width: 120,
          scopedSlots: { customRender: 'identificationCode' },
        },
        {
          title: '资源优先级',
          width: 120,
          dataIndex: 'useResourcePriority',
          scopedSlots: { customRender: 'useResourcePriority' },
        }, {
          title: '计划导弹数量',
          dataIndex: 'planApplyMissileTotal',
          width:120,
          scopedSlots: { customRender: 'planApplyMissileTotal' },
        },
        {
          title: '申请信道数量',
          dataIndex: 'missileUpChannelsNumber',
          width:120,
          scopedSlots: { customRender: 'missileUpChannelsNumber' },
        },
        {
          title:"接收机",
          dataIndex:"receiverType",
          scopedSlots: { customRender: 'receiverType' },
        },
        {
          title: '导弹下行速率',
          width:130,
          dataIndex: 'missileDownRate',
          scopedSlots: { customRender: 'missileDownRate' },

        },

        {
          title: '导弹下行频点',
          dataIndex: 'missileDownFrequency',
          width:130,
          scopedSlots: { customRender: 'missileDownFrequency' },
        },
        {
          title: '导弹上行速率',
          dataIndex: 'missileUpRate',
          scopedSlots: { customRender: 'missileUpRate' },
        },
        {
          title: '操作',
          width: 80,
          dataIndex: 'operation',
          // fixed: 'right',
          scopedSlots: { customRender: 'operation' },
        },
      ],
    }
  },
  created() {
    // this.getList()
  },
  methods: {
    chenggeReceiverType(value){
      this.dataSource.forEach(i=>{
        i.receiverType=value
      })
      if(value=='L'){
        this.pointList=[...this.pointLists.list1]
      }else if(value=='KA'){
        this.pointList=[...this.pointLists.list2]
      }

    },
    changeRate(val){
      this.dataSource.forEach(i=>i.missileDownRate=val)
    },
    changeFrequency(val){
      this.dataSource.forEach(i=>i.missileDownFrequency=val)
    },
    changeMissileUpRate(val) {
      this.dataSource.forEach(i=>i.missileUpRate=val)
    },
    changeCar(record) {
      console.log(record, 'record')
      let select = this.carList.find(i => i.id == record.commandVehicleId)
      record.networkAccessCode = select.networkAccessCode
      record.identificationCode = select.identificationCode

    },
    getList() {
      getAction(this.url.list, { pageSize: -1 }).then((res) => {
        if (res.success && res.result.records && res.result.records.length) {
          this.carList = res.result.records
        } else {
          this.carList = []
        }
      })
    },
    onCellChange(key, dataIndex, value) {
      const dataSource = [...this.dataSource];
      const target = dataSource.find(item => item.key === key);
      if (target) {
        target[dataIndex] = value;
        this.dataSource = dataSource;
      }
    },
    onDelete(key) {
      const dataSource = [...this.dataSource]
      this.dataSource = dataSource.filter((item) => item.key !== key)
      this.$emit('setCount', this.dataSource.length)
      this.count = this.dataSource.length
      console.log(this.dataSource, 'this.dataSource')
    },
    handleAdd() {
      const { count, dataSource } = this
      if (count >= 4) return
      const newData = {
        key: count,
        commandVehicleId: '',
        missileDownFrequency: '55',
        useResourcePriority: '1',
        missileUpChannelsNumber: '4',
        networkAccessCode: '',
        identificationCode: '',
        missileDownRate: '44',
        planApplyMissileTotal: '',
        missileUpRate: '1.5',
        receiverType:"L",
      }
      this.dataSource = [...dataSource, newData]
      this.count = count + 1
      console.log(this.dataSource, 'this.dataSource')
      this.$emit('setCount', this.dataSource.length)
      // this.cacheData = [...this.dataSource]
    },
    showData(record, type) {
      if (type == 'add') {
        this.dataSource = []
        // for (let i = 0; i < record.ncvNum; i++) {
        //   this.dataSource.push({
        //     key: i,
        //     commandVehicleId: '',
        //     missileDownFrequency: '1515',
        //     useResourcePriority: '1',
        //     missileUpChannelsNumber: '4',
        //     networkAccessCode: '',
        //     identificationCode: '',
        //     missileDownRate: '96',
        //     planApplyMissileTotal: '',
        //     missileUpRate: ''
        //   })
        // }
      } else if (type == 'edit') {
         let data = JSON.parse(record)
        data.forEach((i, idx) => {
          i.key = idx
        })
        this.dataSource = [...data]
      }

      this.count = this.dataSource.length
    },
    showModal(record) {
      this.visible = true
      getAction(this.url.getConfig, { fightDemandId: record.id }).then((res) => {
        if (res.success && res.result) {
          if (res.result.length) {
            this.dataSource = res.result
          } else {
            let num = record.ncvNum
            this.dataSource = []
            for (let i = 0; i < num; i++) {
              this.dataSource.push({
                key: i,
                commandVehicleId: '',
                missileDownFrequency: '66',
                useResourcePriority: '1',
                missileUpChannelsNumber: '4',
                networkAccessCode: '',
                identificationCode: '',
                missileDownRate: '96',
                planApplyMissileTotal: '',
                missileUpRate: '1.5',
                receiverType:"L"
              })
            }
          }
        } else {
          this.dataSource = []
        }
      })
      // if (!record.id) {
      //   for (let i = 0; i < num; i++) {
      //     this.dataSource.push({
      //       fightDemandId: record.id,
      //       key: i,
      //       commandVehicleId: '',
      //       missileDownFrequency: '',
      //       useResourcePriority: '',
      //       missileUpChannelsNumber: '',
      //       missileDownRate: '',
      //     })
      //   }
      // } else {
      //   getAction(this.url.getConfig, { id: record.id }).then((res) => {
      //     if (res.success && res.result.records && res.result.records.length) {
      //       this.dataSource = res.result.records
      //     } else {
      //       this.dataSource = []
      //     }
      //   })
      // }
    },
    handleOk(e) {
      let arr = []
      let flag = true
      this.dataSource.forEach((i) => {
        if (!i.commandVehicleId) flag = false
        arr.push(i.commandVehicleId)
      })
      if (!flag) {
        this.$message.error('指控车不可为空！！！')
        return
      } else {
        if (Array.from(new Set(arr)).length < arr.length) {
          this.$message.error('同一辆指控车不可重复添加！！！')
          return
        }
        this.confirmLoading = true
        // return
        httpAction(this.url.config, this.dataSource, 'post').then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.visible = false
            this.confirmLoading = false
          } else {
            this.$message.error(res.message)
            this.visible = false
            this.confirmLoading = false
          }
        })
      }

      // //调新增接口
      // this.$emit('ok', this.dataSource)
      // setTimeout(() => {
      //   this.visible = false
      //   this.confirmLoading = false
      // }, 2000)
    },
    handleCancel(e) {
      console.log('Clicked cancel button')
      this.visible = false
    },
  },
}
</script>

<style></style>
